package com.example.library.vPage;


import com.example.library.util.LogUtil;
import io.github.mthli.slice.shadowlayout.RippleShadowLayout;
import ohos.agp.components.PageSlider;


public class ShadowTransformer implements PageSlider.PageChangedListener {
  private String TAG = "QDX";
  private CardAdapter mAdapter;

  private float mScaleRatio = 0.1f;


  public void attachViewPager(PageSlider pageSlider, CardAdapter adapter) {
    pageSlider.addPageChangedListener(this);
    mAdapter = adapter;

  }

  /**
   * 设置放大倍数，自身为基数。
   */
  public void setZoomIn(float scaleRatio) {
    mScaleRatio = scaleRatio;
  }


  private float mLastOffset;

  private float myPositionOffset;

  @Override
  public void onPageSliding(int position, float positionOffset, int positionOffsetPixels) {
    int realCurrentPosition;
    int nextPosition;
    float realOffset;
    if (positionOffsetPixels <= 0) {
      myPositionOffset = (1 - positionOffset);
    } else {
      myPositionOffset = positionOffset;
    }

    boolean goingLeft = mLastOffset > myPositionOffset;
    LogUtil.error(TAG, "positionOffsetPixels: " + goingLeft);

    // If we're going backwards, onPageScrolled receives the last position
    // instead of the current one
    if (goingLeft) {
      realCurrentPosition = position + 1;
      nextPosition = position;
      realOffset = 1 - myPositionOffset;
    } else {
      nextPosition = position + 1;
      realCurrentPosition = position;
      realOffset = myPositionOffset;
    }


    // Avoid crash on overscroll
    if (nextPosition > mAdapter.getCount() - 1 || realCurrentPosition > mAdapter.getCount() - 1) {
      return;
    }

    RippleShadowLayout currentCard = mAdapter.getCardViewAt(realCurrentPosition);
    // This might be null if a fragment is being used
    // and the views weren't created yet
    if (currentCard != null) {
      currentCard.setScaleX((1 + mScaleRatio * (1 - realOffset)));
      currentCard.setScaleY((1 + mScaleRatio * (1 - realOffset)));
      currentCard.setElevation(mAdapter.getMaxElevationFactor() * (1 - realOffset));
    }
    RippleShadowLayout nextCard = mAdapter.getCardViewAt(nextPosition);
    // We might be scrolling fast enough so that the next (or previous) card
    // was already destroyed or a fragment might not have been created yet
    if (nextCard != null) {
      nextCard.setScaleX((1 + mScaleRatio * (realOffset)));
      nextCard.setScaleY((1 + mScaleRatio * (realOffset)));
      nextCard.setElevation(mAdapter.getMaxElevationFactor() * (realOffset));
    }


    if (realOffset == 1) {  //适用于：pos从0->3 ,过程中realOffset并不能至于0，所以伸缩会受到影响
      RippleShadowLayout cardView = null;
      if (goingLeft && nextPosition + 2 < mAdapter.getCount()) {
        cardView = mAdapter.getCardViewAt(nextPosition + 2);
      } else if (goingLeft && nextPosition - 2 > 0) {
        cardView = mAdapter.getCardViewAt(nextPosition - 2);
      }
      if (cardView != null) {
        cardView.setElevation(0);
        cardView.setScaleX((1));
        cardView.setScaleY((1));
      }
    }

    mLastOffset = myPositionOffset;


  }

  @Override
  public void onPageSlideStateChanged(int i) {

  }

  @Override
  public void onPageChosen(int i) {

  }
}
